import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    meta: {
      menu: true,
      keep: true
    },
    component: HomeView
  },

  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import("@/views/DetailView.vue")
  },
  {
    path: '/cate',
    name: 'cate',
    meta: {
      menu: true,
      keep: true
    },
    component: () => import("@/views/CategoryView.vue")
  },
  {
    path: '/goodsList/:id',
    name: 'goodsList',
    component: () => import("@/views/GoodsListView.vue")
  },
  {
    path: '/live',
    name: 'live',
    meta: {
      menu: true
    },
    component: () => import("@/views/LiveView.vue")
  },
  {
    path: '/cart',
    name: 'cart',
    meta: {
      menu: true,
      requireAuth: true
    },
    component: () => import("@/views/CartView.vue")
  },
  {
    path: '/user',
    name: 'user',
    meta: {
      menu: true
    },
    component: () => import("@/views/user/UserView.vue")
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/user/Login.vue")
  },
  {
    path: '/register',
    name: 'register',
    component: () => import("@/views/user/RegisterView.vue")
  },
  {
    path: '/search',
    name: 'search',
    component: () => import("@/views/SearchView.vue")
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
   { path: '/:pathMatch(.*)*', name: 'NotFound', component: ()=>import("@/views/404.vue") },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (sessionStorage.getItem("islog")) {
      next(true)
    } else {
      next("/login?redirect=" + to.path)
    }
  } else {
    next(true)
  }
}
)

export default router
